/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define HUDMODULE
#include "HUDSlider.h"

namespace ModularEngine
{

	HUDSlider::HUDSlider()
	{
		LOADCOLORS;
		// Set default values if user doen't submit them.
		mMinValue = 0;
		mMaxValue = 100;
		mCurrentValue = 0;
	}

	HUDSlider::~HUDSlider()
	{

	}

	int HUDSlider::initialize()
	{
		return 1;
	}

	void HUDSlider::draw()
	{
		// if the mousedown bool is set to true still, adjust the slider.
		// unless, of course, the mouse has been released.
		if( mbMouseDown )
		{
			if( WindowModule::getSingleton().isDown( 0 ) )
				adjustSlider();
			else
				mbMouseDown = false;
		}
		gl->resetCoords();

		// Draw border
		glColor3f( mvTextColor.x*.8, mvTextColor.y*.8, mvTextColor.z*.8 );
		gl->drawRect( Vector2d( mvPosition.x-1, mvPosition.y+mvDimensions.y/3-1 ), Vector2d( mvDimensions.x+2, mvDimensions.y/3+2 ) );

		// Draw Slide
		gl->drawGradientRect( Vector2d( mvPosition.x, mvPosition.y+mvDimensions.y/3 ), Vector2d( mvDimensions.x, mvDimensions.y/3 ), mvBaseColor, mvBaseColor*.5 );

		// calculate slider position based on value
		float xPosition = mCurrentValue/(abs(mMinValue)+abs(mMaxValue));

		// Draw the slider
		gl->drawGradientRect( Vector2d( mvPosition.x+xPosition*mvDimensions.x-1, mvPosition.y ), Vector2d( 3, mvDimensions.y ), mvTextColor, mvTextColor*.7 );


	}

	void HUDSlider::setSlider(float fMin, float fMax, float fStartPos)
	{
		// Set appropriate values
		mMinValue = fMin;
		mMaxValue = fMax;
		mCurrentValue = fStartPos;
	}

	void HUDSlider::mouseInput(ModularEngine::Vector2d vPos, UINT nDown)
	{
		// adjust slider
		mbMouseDown = true;
		adjustSlider();
	}

	void HUDSlider::keyboardInput( UINT nKey )
	{

	}

	void HUDSlider::adjustSlider()
	{
		// get mouse position and adjust it relative to the slider position
		Vector2d vMouse = WindowModule::getSingleton().getMouse();
		
		float xPercent = (vMouse.x-mvPosition.x)/mvDimensions.x;
		if( xPercent > 1 )
			xPercent = 1;
		if( xPercent < 0 )
			xPercent = 0;

		// Set the current value based on cursor position
		mCurrentValue = xPercent * (abs(mMinValue)+abs(mMaxValue));
	}

	float HUDSlider::getValue()
	{
		return mCurrentValue;
	}

}
